package com.ifly.voice.service;

import com.ifly.voice.app.Logs;
import com.ifly.voice.recognizer.RecognitionListener;
import com.ifly.voice.recognizer.RecognizerTaskHelper;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class VoiceService extends Service
{

	@Override
	public IBinder onBind(Intent arg0)
	{
		return voiceListener;
	}

	VoiceListener voiceListener = new VoiceListener();

	class VoiceListener extends Binder implements IVoiceListener
	{

		private RecognizerTaskHelper helper;

		public VoiceListener()
		{
			helper = new RecognizerTaskHelper();
		}

		@Override
		public void setRecognitionListener(RecognitionListener recognitionListener)
		{
			Logs.d("setRecognitionListener recognitionListener="+recognitionListener);
			helper.setRecognitionListener(recognitionListener);
		}

		boolean hasStarted = false;
		boolean hasListened = false;
		@Override
		public synchronized void startThread()
		{
			if (!hasStarted)
			{
				helper.startThread();
				hasStarted = true;
			}
			
		}

		@Override
		public void startListen()
		{
			if (!hasListened)
			{
				hasListened=true;
				helper.startListen();
			}
			
			
		}

		@Override
		public void stopListen()
		{
			if (hasListened)
			{
				hasListened = false;
				helper.stopListen();
			}
			
			
		}

	}

}